package com.service.edu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.service.edu.pojo.Course;
import com.service.edu.pojo.Teacher;
import com.service.edu.pojo.vo.TeacherQuery;
import com.service.edu.service.CourseService;
import com.service.edu.service.TeacherService;
import com.service.utils.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-09-18
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private CourseService courseService;

    @GetMapping("limitOrderByTime")
    @ApiOperation("查询最新前4条讲师数据")
    @Cacheable(value = "guli:index:teacher",key = "'list'")
    public ResultVo limitOrderByTime(){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc("gmt_create");
        queryWrapper.last("limit 4");
        List<Teacher> list = teacherService.list(queryWrapper);
        return ResultVo.ok().data("items",list);
    }


    @PostMapping("page")
    @ApiOperation("分页查询所有的讲师信息")
    public ResultVo page(
         @ApiParam(name = "pageNum",value = "当前页数")  @RequestParam(defaultValue = "1",value = "pageNum")  Integer pageNum,
         @ApiParam(name = "rows",value = "每页显示条数")  @RequestParam(defaultValue = "10",value = "rows") Integer rows,
         @ApiParam(name = "teacherQuery",value = "讲师查询条件",required = false) @RequestBody(required = false) TeacherQuery teacherQuery
    ){
        return teacherService.page(pageNum,rows,teacherQuery);
    }

    @GetMapping
    @ApiOperation("查询所有讲师信息")
    public ResultVo list(){
        List<Teacher> list = teacherService.list(null);
        return ResultVo.ok().data("items",list);
    }

    /**
     * 查询指定的讲师信息
     * @param id
     * @return
     */
    @ApiOperation("查询指定的讲师信息")
    @GetMapping("{id}")
    public  ResultVo queryById(@ApiParam(name = "id",value = "指定查询的讲师ID",required = true) @PathVariable String id){
        Teacher teacher = teacherService.getById(id);
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("teacher_id",teacher.getId());
        teacher.setCourseList(courseService.list(queryWrapper));
        return ResultVo.ok().data("item",teacher);
    }

    /**
     * 删除指定的讲师信息
     * @param id
     * @return
     */
    @CacheEvict(value = "guli:index:teacher",key = "'list'")
    @ApiOperation("逻辑删除讲师信息")
    @DeleteMapping("{id}")
    public ResultVo delete(@ApiParam(name = "id",value = "逻辑删除的讲师ID",required = true) @PathVariable String id){
        boolean b = teacherService.removeById(id);
        if (b){
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    /**
     * 修改讲师信息
     * @param teacher
     * @return
     */
    @CacheEvict(value = "guli:index:teacher",key = "'list'")
    @ApiOperation("修改讲师信息")
    @PutMapping
    public ResultVo update(@RequestBody Teacher teacher){
        teacher.setGmtModified(new Date());
        boolean b = teacherService.updateById(teacher);
        if (b){
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    /**
     * 添加讲师信息
     * @param teacher
     * @return
     */
    @CacheEvict(value = "guli:index:teacher",key = "'list'")
    @ApiOperation("添加讲师信息")
    @PostMapping
    public ResultVo save(@RequestBody Teacher teacher){
        teacher.setGmtModified(new Date());
        teacher.setGmtCreate(new Date());
        boolean b = teacherService.save(teacher);
        if (b){
            return ResultVo.ok();
        }
        return ResultVo.error();
    }




}

